﻿namespace SLClient.CustomerControls
{
    using SLClient.Extension;
    using SLClient.Infrastructure;
    using SLClient.ServiceAPI;
    using System;
    using System.Collections.ObjectModel;
    using System.Linq.Expressions;
    using System.Reflection;
    using System.Runtime.CompilerServices;
    using System.Windows;
    using System.Windows.Input;

    public class MoneyOutConfirm : ViewModelBase
    {
        private ViwMoneyOut _currentViwMoneyOut;
        private string _errMsg;
        private Visibility _isVisibility;
        private Visibility _processVisibity;
        private ObservableCollection<ViwMoneyOut> _viwMoneyouts;

        public MoneyOutConfirm(ViwMoneyOut viwMoneyOut, ObservableCollection<ViwMoneyOut> viwMoneyOuts)
        {
            this.IsVisibility = 0;
            this._currentViwMoneyOut = viwMoneyOut;
            this._viwMoneyouts = viwMoneyOuts;
            this.ProcessVisibity =Visibility.Collapsed;
            this.CommandInit();
        }

        public void CommandInit()
        {
            RelayCommand command = new RelayCommand(new Action(this.Save)) {
                IsEnabled = true
            };
            this.SaveCommand = command;
        }

        public void Save()
        {
            this.ErrMsg = "";
            this.ProcessVisibity = 0;
            ServiceClient instance = WCFServicesUtil<ServiceClient>.GetInstance(ServiceType.ServiceAPI);
            instance.UpdateUserActivityMoneyByUserIdCompleted += delegate (object obj, UpdateUserActivityMoneyByUserIdCompletedEventArgs arg) {
                if (arg.Result)
                {
                    this.ErrMsg = "转账成功";
                    this._viwMoneyouts.Remove(this._currentViwMoneyOut);
                    this.IsVisibility = Visibility.Collapsed;
                }
                else
                {
                    this.ErrMsg = "转账无效,无法读取有效信息";
                }
                this.ProcessVisibity = Visibility.Collapsed;
            };
            instance.UpdateUserActivityMoneyByUserIdAsync(this._currentViwMoneyOut);
        }

        public ViwMoneyOut CurrentViwMoneyOut
        {
            get
            {
                return this._currentViwMoneyOut;
            }
            set
            {
                this._currentViwMoneyOut = value;
                base.Notify<ViwMoneyOut>(() => this.CurrentViwMoneyOut);
                //base.Notify<ViwMoneyOut>(System.Linq.Expressions.Expression.Lambda<Func<ViwMoneyOut>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(MoneyOutConfirm)), (MethodInfo)(MoneyOutConfirm.get_CurrentViwMoneyOut)), new ParameterExpression[0]));
            }
        }

        public string ErrMsg
        {
            get
            {
                return this._errMsg;
            }
            set
            {
                this._errMsg = value;
                base.Notify<string>(() => this.ErrMsg);
                //base.Notify<string>(System.Linq.Expressions.Expression.Lambda<Func<string>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(MoneyOutConfirm)), (MethodInfo)(MoneyOutConfirm.get_ErrMsg)), new ParameterExpression[0]));
            }
        }

        public Visibility IsVisibility
        {
            get
            {
                return this._isVisibility;
            }
            set
            {
                this._isVisibility = value;
                base.Notify<Visibility>(() => this.IsVisibility);
                //base.Notify<Visibility>(System.Linq.Expressions.Expression.Lambda<Func<Visibility>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(MoneyOutConfirm)), (MethodInfo)(MoneyOutConfirm.get_IsVisibility)), new ParameterExpression[0]));
            }
        }

        public Visibility ProcessVisibity
        {
            get
            {
                return this._processVisibity;
            }
            set
            {
                this._processVisibity = value;
                base.Notify<Visibility>(() => this.ProcessVisibity);
                //base.Notify<Visibility>(System.Linq.Expressions.Expression.Lambda<Func<Visibility>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(MoneyOutConfirm)), (MethodInfo)(MoneyOutConfirm.get_ProcessVisibity)), new ParameterExpression[0]));
            }
        }

        public ICommand SaveCommand { get; set; }
    }
}

